
const tool = {

    lineEchart(model, ref, xdata, ydata) {
        var myChart = echarts.init(model.$refs[ref]);

        var option = {
            xAxis: {
                type: 'category',
                data: xdata
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    data: ydata,
                    type: 'line'
                }
            ]
        };

        option && myChart.setOption(option);
    },

    async onSubmit(model) {
        model.formInline.cpage = 1
        await tool.loadTable(model)
    },

    handleSelectionChange(model, val) {
        model.multipleSelection = val
    },

    async handleSizeChange(model, val) {
        model.csize = val
        model.formInline.csize = model.csize
        await tool.loadTable(model)
    },

    async handleCurrentChange(model, val) {
        model.cpage = val
        model.formInline.cpage = model.cpage
        await tool.loadTable(model)
    },

    async onReset(model) {
        model.formInline = {}

        Object.keys(model.df).forEach(_ => model.formInline[_] = model.df[_])

        model.formInline.env = ''
        model.cpage = 1
        model.csize = 10
        await tool.loadTable(model)
    },

    async deleteBtn(model) {
        if (model.multipleSelection.length === 0) {
            model.$message.error('请至少选择一条删除')
            return false
        }
        const ids = model.multipleSelection.map(_ => _.id).join(',')
        const res = await axios.delete(model.prefixLink +  model.moduleLink + '/delete_more?ids=' + ids, {
            headers: {
                "Auth": "BASIC " + localStorage.getItem("TOKEN")
            }
        })
        if (200 == res.status && res.data.code === 200) {
            await tool.loadTable(model)
        }

    },

    async copyBtn(model) {
        if (model.multipleSelection.length !== 1) {
            model.$message.error('请选择一条复制')
            return false
        }
        const { id } = model.multipleSelection[0]
        const res = await axios.post(model.prefixLink +  model.moduleLink + '/copy/' + id, {}, {
            headers: {
                "Auth": "BASIC " + localStorage.getItem("TOKEN")
            }
        })
        if (200 == res.status && res.data.code === 200) {
            await tool.loadTable(model)
        }

    },

    async updateBtn(model) {
        if (model.multipleSelection.length !== 1) {
            model.$message.error('请选择一条修改')
            return false
        }
        const { id } = model.multipleSelection[0]
        const res = await axios.get(model.prefixLink +  model.moduleLink + '/one/' + id,{
            headers: {
                "Auth": "BASIC " + localStorage.getItem("TOKEN")
            }
        })
        if (200 == res.status && res.data.code === 200) {
            model.updateForm = res.data.data
            model.updateDialogShow = true
        }
    },

    async handleUpdate(model) {
        const res = await axios.put(model.prefixLink +  model.moduleLink + '/update', model.updateForm, {
            headers: {
                "Auth": "BASIC " + localStorage.getItem("TOKEN")
            }
        })
        if (200 === res.status && 200 === res.data.code) {
            model.updateForm = {}
            model.updateDialogShow = false
            await tool.loadTable(model)
        }
    },

    async switchStatus(model, row) {
        const {id, status} = row
        const res = await axios.put(model.prefixLink +  model.moduleLink  + '/status/' + status + '/' + id)
        if (200 === res.status && 200 === res.data.code) {
            await tool.loadTable(model)
        }
    },


    async justGet(model, url, params, cbFunc) {
        const res = await axios.get(model.prefixLink +  model.moduleLink  + url, {
            params: params ,
            headers: {
                "Auth": "BASIC " + localStorage.getItem("TOKEN")
            }
        })
        if (200 === res.status && 200 === res.data.code) {
            cbFunc(res.data.data)
        }
    },

    async justPost(model, url, data, cbFunc) {
        const res = await axios.post(model.prefixLink +  model.moduleLink  + url,
            data,
            {
                headers: {
                    "Auth": "BASIC " + localStorage.getItem("TOKEN")
                }
            }
        )
        if (200 === res.status && 200 === res.data.code) {
            cbFunc(res.data.data)
        }
    },

    async loadTable(model) {
        const res = await axios.get(model.prefixLink +  model.moduleLink +  '/all', {
            params: model.formInline,
            headers: {
                "Auth": "BASIC " + localStorage.getItem("TOKEN")
            }
        })
        if (200 === res.status && 200 === res.data.code) {
            const info = res.data.data
            model.list = info.list
            model.total = info.total
        } else {
            model.$message.error(res.data.code + " : " + res.data.message)
        }
    },

    openUrl(url) {
        window.open(url)
    },

    datepicker() {
        return {
            shortcuts: [{
                text: '最近一周',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                    picker.$emit('pick', [start, end]);
                }
            }, {
                text: '最近一个月',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                    picker.$emit('pick', [start, end]);
                }
            }, {
                text: '最近三个月',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                    picker.$emit('pick', [start, end]);
                }
            }]
        }
    }

} ;